define(['moment', 'JDA'], function (moment, JDA) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', '$timeout', 'config','$calculater',
    function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, $timeout, config, $calculater) {
            // (原本)0: 加工, 1: 閒置, 2: 故障, 3: 保養, 4: 關機, 5:暫停, 6: 維修, 7: 設置
            // var status_code = {'MACHINING': 0, 'IDEAL': 1, 'FAULT': 2, 'MAINTENANCE': 3, 'SHUTDOWN': 4, 'PAUSE': 5, 'RESTORATION': 6, 'SETUP': 7};

            // (淨徨版本) 0: 閒置, 1: 加工, 2: 故障, 3: 維修, 4: 保養, 5: 暫停, 6:設置, 7: 關機
            var status_code = {'IDEAL': 0, 'MACHINING': 1, 'FAULT': 2, 'RESTORATION': 3, 'MAINTENANCE': 4, 'PAUSE': 5, 'SETUP': 6, 'SHUTDOWN': 7};

            $scope.pieChart_config = {
                innerRadiusRate: 0.6,
                isShowLabel: false,
                radius: 80
            };
            $scope.pie_data_users = [
                // {id: 'TOTALEQUIPMENT', label: $filter('translate')('mobile08.unwork_users'), color: '#68bd84', value: 0 },
                {id: 'MACHINING', label: $filter('translate')('mobile08.machine_status.MACHINING'), color: '#68bd84', value: 0, focus:false }, //加工
                {id: 'IDEAL', label: $filter('translate')('mobile08.machine_status.IDEAL'), color: '#f6cf5a', value: 0, focus:false }, //閒置
                {id: 'FAULT', label: $filter('translate')('mobile08.machine_status.FAULT'), color: '#db4b3c', value: 0, focus:false }, //故障
                {id: 'MAINTENANCE', label: $filter('translate')('mobile08.machine_status.MAINTENANCE'), color: '#e67432', value: 0, focus:false }, //保養
                {id: 'SHUTDOWN', label: $filter('translate')('mobile08.machine_status.SHUTDOWN'), color: '#666666', value: 0, focus:false }, //關機
                {id: 'PAUSE', label: $filter('translate')('mobile08.machine_status.PAUSE'), color: '#ba55d3', value: 0, focus:false }, //暫停
                {id: 'RESTORATION', label: $filter('translate')('mobile08.machine_status.RESTORATION'), color: '#4f74b9', value: 0, focus:false }, //維修
                {id: 'SETUP', label: $filter('translate')('mobile08.machine_status.SETUP'), color: '#ff69b4', value: 0, focus:false } //設置
            ];
            $scope.query_result = [];

            $scope.init = function(){
                init();
            };

            $scope.back = function(){
                $rootScope.changeProgram('menu', {}, true);
            };

            $scope.getDashboardLabel = function(item){
                return item.label + ' (' + getPercentScale(item.value) + '%)';
            };

            var total_basis_data_qty = 0;
            $scope.search = function(callback){
                $scope.query_result = [];
                getBasisData(function(report_data){
                    total_basis_data_qty = report_data['TOTALEQUIPMENT'];
                    $scope.pie_data_users.forEach(function(pie_data){
                        pie_data.value = report_data[pie_data.id];
                        $scope.query_result.push({'ID': pie_data.id, 'QTY': report_data[pie_data.id]});
                    });
                    $scope.showItemDetail($scope.query_result[0]);
                });
            };

            // 資料細節開窗
            $scope.showItemDetail = function(item){
                $scope.itemDetail = [];
                $scope.pie_data_users.forEach(function(pie_data){
                    if(pie_data.id == item.ID)
                        pie_data.focus = true
                    else
                        pie_data.focus = false;
                });
                getMachineList($scope.getStatusCode(item.ID), function(data){
                    $scope.itemDetail = data;
                });
            };

            function init(){
                $scope.search();
            }

            function getPercentScale(value){
                return $calculater.decimalMutiple((+value / +total_basis_data_qty), 100).toPrecision(2);
            }

            //取得設備現況基本資料
            function getBasisData(callback){
                var content = {
                    report_id: 'SR02',
                    query_condition_info: []
                };
                userCustomReportDataGet(content, function(data){
                    $scope.loading_time = moment().format('YYYY-MM-DD HH:mm:ss');
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data[0]);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            $scope.machineStatus = function (status) {
                return 'status_'+status;
            };

            // 轉換機台狀態代號
            $scope.getStatusCode = function(id){
                return status_code[id];
            }

            // 透過狀態取得機台清單
            function getMachineList(status, callback){
                var content = {
                    report_id: 'SR0201',
                    query_condition_info: [
                        {column_name: "a.EQUIPMENTSTATE", query_mode: "0", merge_condition_model: "AND", value: status}
                    ]
                };
                userCustomReportDataGet(content, function(data){
                    if(data.report_data && data.report_data.length > 0){
                        if (callback)
                            callback(data.report_data);
                    } else {
                        $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
            }

            function userCustomReportDataGet(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_SYS.user_custom_report_data_get',
                    content : content,
                    success : function(data){
                        if(callback){
                            callback(data);
                        }
                    }
                });
            }
        }
    ];
});
